﻿<?php

//就近 www.jiujin.me
global $_W, $_GPC;
$mid = intval($_GET['mid']);
$openid = m('user')->getOpenid();
$member = m('member')->getInfo($openid);
$shop_set = m('common')->getSysset('shop');
$can = false;
if ($member['isagent'] == 1 && $member['status'] == 1) {
	$can = true;
}
//if (!$can) {
//	header("location: " . $this->createPluginMobileUrl('commission/register'));
//	die;
//}
$returnurl = urlencode($this->createPluginMobileUrl('commission/shares', array('goodsid' => $_GPC['goodsid'])));
$infourl = "";
$set = $this->set;
if (empty($set['become_reg'])) {
	if (empty($member['realname']) || empty($member['mobile'])) {
		//$infourl = $this->createMobileUrl('member/info', array('returnurl' => $returnurl));
	}
}
/*
*  gwj
*  修改：会员发展会员
*  时间：2016年10月8日
*/
if($mid != $member['id']){
       if(!$member['agentid']){
               //更新为下级
               $parent=pdo_fetch('select * from'.tablename('ewei_shop_member')."where id='{$mid}' and uniacid='{$_W['uniacid']}'");
               $set = $this->getSet();
               $tm = $set['tm'];
               $templateid = $tm['templateid'];
               $message = $tm['commission_agent_new'];
               $msg = array('keyword1' => array('value' => !empty($tm['commission_agent_newtitle']) ? $tm['commission_agent_newtitle'] : '新增下线通知', "color" => "#73a68d"), 'keyword2' => array('value' => $message, "color" => "#73a68d"));
               if (!empty($templateid)) {
                        m('message')->sendTplNotice($parent['openid'], $templateid, $msg);
                } else {
                        m('message')->sendCustomNotice($parent['openid'], $msg);
                }
                pdo_query('update ' . tablename('ewei_shop_member') . " set agentid='{$mid}' where id=:id and uniacid='{$_W['uniacid']}' ", array(":id" => $member['id']));
       }
}
if (empty($infourl)) {
	$myshop = $this->model->getShop($member['id']);
	$share_goods = false;
	$share = array();
	$goodsid = intval($_GPC['goodsid']);
	if (!empty($goodsid)) {
		$goods = pdo_fetch('select * from ' . tablename('ewei_shop_goods') . ' where uniacid=:uniacid and id=:id limit 1', array(':uniacid' => $_W['uniacid'], ':id' => $goodsid));
		$goods = set_medias($goods, 'thumb');
		if (!empty($goods)) {
			$commission = number_format($this->model->getCommission($goods), 2);
			$share_goods = true;
			$_W['shopshare'] = array('title' => !empty($goods['share_title']) ? $goods['share_title'] : $goods['title'], 'imgUrl' => !empty($goods['share_icon']) ? tomedia($goods['share_icon']) : tomedia($goods['thumb']), 'desc' => !empty($goods['description']) ? $goods['description'] : $myshop['name'], 'link' => $this->createMobileUrl('shop/detail', array('id' => $goods['id'], 'mid' => $member['id']), true));
		}
	}
	if (!$share_goods) {
		
		//新的分销分享
		$setdata = pdo_fetch("select * from " . tablename('ewei_shop_sysset') . ' where uniacid=:uniacid limit 1', array(':uniacid' => $_W['uniacid']));
		$set = unserialize($setdata['sets']);
		
		if (empty($_GET['mid'])) {
			//$_W['shopshare'] = array('imgUrl' => tomedia('images/5/2015/12/NlwhwswvWPd6TdSd8gDHdLHLgd46Dt.jpg'), 'title' => '欢迎加入小妖家', 'desc' => '快来加入小妖家吧！', 'link' => $this->createMobileUrl('shop', array('mid' => $_GPC['mid'])));
			//$shop = $this->model->getShop($_GPC['mid']);
			$myshop = $this->model->getShop($member['id']);
			//$_W['shopshare'] = array('imgUrl' => $shop['logo'], 'title' => $shop['name'], 'desc' => $shop['desc'], 'link' => $this->createPluginMobileUrl('commission/myshop', array('mid' => $shop['mid']), true));
			$_W['shopshare'] = array('imgUrl' =>tomedia('images/5/2016/10/BBVfz7tg1V71778WVe7VZb0TcpGG11.jpg'), 'title' =>$myshop['name'], 'desc' => $myshop['desc'], 'link' => $this->createPluginMobileUrl('commission/shares', array('mid' => $shop['mid']), true));
		} else {
                       	$myshop = $this->model->getShop($_GET['mid']);
			//$_W['shopshare'] = array('imgUrl' => tomedia('images/5/2015/12/NlwhwswvWPd6TdSd8gDHdLHLgd46Dt.jpg'), 'title' => '欢迎加入小妖家', 'desc' => '快来加入小妖家吧！', 'link' => $this->createMobileUrl('shop', array('mid' => $member['id'])));
			$_W['shopshare'] = array('imgUrl' =>tomedia('images/5/2016/10/BBVfz7tg1V71778WVe7VZb0TcpGG11.jpg'), 'title' => $myshop['name'], 'desc' => $myshop['desc'], 'link' => $this->createPluginMobileUrl('commission/shares', array('mid' =>$_GET['mid']), true));
		}
	}
}
if (empty($infourl) && $_W['isajax']){
	$p = p('poster');
	if ($share_goods) {
		if ($p) {
			$img = $p->createCommissionPoster($openid, $goods['id']);
			//$img = '../attachment/'.$goods['share_icon'];
		}
		if (empty($img)) {
			$img = $this->model->createGoodsImage($goods, $shop_set);
		}
	} else {
		if ($p) {
			$img = $p->createCommissionPoster($openid,$goodsid = 0,$_GET['mid']);
		}
		if (empty($img)) {
			$img = $this->model->createShopImage($shop_set);
		}
	}
	/*
	 *  gmm
	 *  修改：我的推广二维码
	 *  时间：2015年11月24日
	 */
        if(empty($_GET['mid'])){
	$img = pdo_fetch("SELECT qrimg FROM " . tablename('ewei_shop_poster_qr') . " WHERE acid = :acid AND openid = :openid AND type=4 and news=1 order by id desc ", array(':acid' => $_W['uniacid'],':openid'=>$openid));
        }else{
        $info= pdo_fetch('select * from ' . tablename('ewei_shop_member') . ' where uniacid=:uniacid and id=:id limit 1', array(':uniacid' => $_W['uniacid'], ':id' =>$_GET['mid']));
        $img = pdo_fetch("SELECT qrimg FROM " . tablename('ewei_shop_poster_qr') . " WHERE acid = :acid AND openid = :openid AND type=4 and news=1 order by id desc ", array(':acid' => $_W['uniacid'],':openid'=>$info['openid']));    
        }
	$img = $img['qrimg'];
	die($img);
}
include $this->template('shares');